﻿package Gameplay.Cells 
{
	import Gameplay.Constants;
	import Gameplay.GridContent.ContentAbstract;
	
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class GridCell extends Sprite 
	{
		private const CSize:int = Constants.CELLS_COUNT;		
		private const CLineAlpha:Number = Constants.DEFAULT_GRID_ALPHA;
		private const CMissedColor:uint = 0x666666;		
		
		private var FColorized:Boolean;
		private var FShape:Shape;
		private var FWidth:Number;
		private var FHeight:Number;
		private var FLineColor:uint;
		private var FLineAlpha:Number;
		private var FColor:uint;
		private var FRow:int;
		private var FColumn:int;
		private var FContent:ContentAbstract;		
		
		public function GridCell():void
		{
			FShape = new Shape();
			addChild(FShape);
			FWidth = new Number();
			FHeight = new Number();
			FLineColor = Constants.DEFAULT_GRID_COLOR;
			FLineAlpha = CLineAlpha;
			FRow = -1;
			FColumn = -1;
			FContent = null;
			addEventListener(MouseEvent.MOUSE_OVER, OnMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, OnMouseOut);
			addEventListener(MouseEvent.MOUSE_DOWN, OnMouseDown);
			addEventListener(Event.REMOVED_FROM_STAGE, OnRemovedFromStage);
		}
		
		private function OnRemovedFromStage(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, OnRemovedFromStage);
			graphics.clear();
			FShape.graphics.clear();
			removeChild(FShape);
		}
				
		private function OnMouseOver(e:MouseEvent):void 
		{
			FShape.alpha = CLineAlpha;
		}
		
		private function OnMouseOut(e:MouseEvent):void 
		{
			FColorized ? FShape.alpha = CLineAlpha : FShape.alpha = 0;
		}
		
		private function OnMouseDown(e:MouseEvent):void 
		{
			if (!FColorized)
			{
				if (Content)
				{
				}
				else
				{
				//	Color = CMissedColor;
				}
			}
		}
		
		public function Draw():void
		{
			Fill(graphics, FLineAlpha);
			Fill(FShape.graphics, 0, 1);
			FShape.alpha = 0;			
			cacheAsBitmap = true;
			FShape.cacheAsBitmap = true;
		}
		
		private function Fill(ATarget:Graphics, ALinesAlpha:Number, AAlpha:Number = 0, AColor:uint = 0xFFFFFF):void
		{
			ATarget.clear();
			ATarget.beginFill(AColor, AAlpha);					
			ATarget.moveTo(0, FHeight / 2);					
			ATarget.lineStyle(1, FLineColor, ALinesAlpha * int(FColumn >= 0));
			ATarget.lineTo(FWidth / 2, 0);
			ATarget.lineStyle(1, FLineColor, ALinesAlpha * int(FRow >= 0));
			ATarget.lineTo(FWidth, FHeight / 2);
			ATarget.lineStyle(1, FLineColor, ALinesAlpha * int(FColumn == CSize - 1));
			ATarget.lineTo(FWidth / 2, FHeight);
			ATarget.lineStyle(1, FLineColor, ALinesAlpha * int(FRow == CSize - 1));					
			ATarget.lineTo(0, FHeight / 2);
			ATarget.endFill();	
		}
			
		public function Assign(ACell:GridCell):void
		{
			if (ACell)
			{
				FContent = ACell.Content;
				if (ACell.IsColorized)			
					Color = ACell.Color;				
			}
		}
		
		public function get Row():int 
		{
			return FRow;
		}
		
		public function set Row(value:int):void 
		{
			FRow = value;
		}
		
		public function get Column():int 
		{
			return FColumn;
		}
		
		public function set Column(value:int):void 
		{
			FColumn = value;
		}	
		
		public function set CellWidth(value:Number):void 
		{
			FWidth = value;
		}
		
		public function set CellHeight(value:Number):void 
		{
			FHeight = value;
		}
		
		public function get Color():uint
		{
			return FColor;
		}
		
		public function set Color(AColor:uint):void
		{
			FColor = AColor;
			Fill(FShape.graphics, 0, 1, AColor);
			FColorized = true;
			FShape.alpha = CLineAlpha;
		}
		
		public function get IsColorized():Boolean
		{
			return FColorized;
		}
		
		public function get Content():ContentAbstract 
		{
			return FContent;
		}
		
		public function set Content(AContent:ContentAbstract):void 
		{
			FContent = AContent;
		}
	}
}